//
//  JBNetWorkTools.swift
//  sinaBlog
//
//  Created by jobo on 15/11/1.
//  Copyright © 2015年 jobo. All rights reserved.
//

import UIKit
import AFNetworking


class JBNetWorkTools: AFHTTPSessionManager {
    
    
    
    //单例
    static let shareInstance : JBNetWorkTools =
    {
        let url = "https://api.weibo.com/"
        
        let tools = JBNetWorkTools(baseURL: NSURL(string: url))
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")

        
        return tools
    
    }()

    
    //838811656
    //https://www.baidu.com/
    
    
    //key
    let client_id = "838811656"
    
    let client_secret = "be0ae6b9e094d7b01e71111992f92f98"
    //回调地址 
    let redirect_uri = "http://www.baidu.com/"
    
    let grant_type = "authorization_code"
    
    //拼接请求地址 oauthUrl地址
    
    func oauthURL() -> NSURL
    {
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
      //let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"

        
        return NSURL(string: urlString)!
    }
    
    // 使用闭包回调
    // MARK: - 加载AccessToken
    /// 加载AccessToken
    func loadAccessToken(code: String, finshed: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        // url
        let urlString = "oauth2/access_token"
        
        // NSObject
        // AnyObject, 任何 class
        // 参数
        let parameters = [
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": grant_type,
            "code": code,
            "redirect_uri": redirect_uri
        ]
        
        // 测试返回结果类型
        //        responseSerializer = AFHTTPResponseSerializer()
        // result: 请求结果
        POST(urlString, parameters: parameters, success: { (_, result) -> Void in
            
            //            let data = String(data: result as! NSData, encoding: NSUTF8StringEncoding)
            //            print("data: \(data)")
            
            finshed(result: result as? [String: AnyObject], error: nil)
            }) { (_, error: NSError) -> Void in
                finshed(result: nil, error: error)
        }
    }
    
    //加载用户的信息
    func loadUserInfo (finished : (result : [String : AnyObject]? , error : NSError? ) -> ())
    {
        if JBUserAccount.loadAccount()?.access_token == nil
        {
            print("tooken 为空")
            return
        } 
        if JBUserAccount.loadAccount()?.uid == nil
        {
            print("uid 为空")
            return
        }
        
        let userInfoUrl = "https://api.weibo.com/2/users/show.json"
        

        
        let parameters = [
            "access_token": JBUserAccount.loadAccount()!.access_token!,
            "uid": JBUserAccount.loadAccount()!.uid!
        ]
        
        print(parameters)

        
        GET(userInfoUrl, parameters: parameters, success: { (_, result) -> Void in
            
            print("=======请求成功 result : \(result)")
            
            finished(result: result as? [String : AnyObject], error: nil)
            
            }) { (_, error) -> Void in
                print("请求失败 result : \(error)")
                finished(result: nil, error: error)
        }
        
        
        
    }
    
    func loadStatus(finished : (result : [String : AnyObject]? , error: NSError? ) -> ())
    {
        if(JBUserAccount.loadAccount()?.access_token == nil)
        {
            print("access_token为空")
            
            return
        }
        
        print("access_token 有值")
        
        //请求链接
        print(JBUserAccount.loadAccount()!.access_token!)
        
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parameters = ["access_token": JBUserAccount.loadAccount()!.access_token!]
        
        GET(urlStr, parameters: parameters, success: { (_, result) -> Void in
            print("获取微博数据请求成功")
            finished(result: result as? [String : AnyObject], error: nil)
            }) { (_, error) -> Void in
                print("获取微博数据请求失败")
                finished(result: nil, error: error)
        }
        
    
    }
    
    
    
}
